zingchart.setModule("heatmap"),function(){if(zingchart.plugins.heatmap=zingchart.plugins.heatmap||{},!ZC.ZCHeatMap){var t={};ZC.ZCHeatMap=function(){this.AC="canvas",this.I=0,this.F=0,this.aDataInit=null,this.aData=[],this.oKVData={},this.aSize=[15,15],this.iBlur=45,this.LG=!1,this.A8=null,this.bComposite=!0,this.aGradient=[[.4,"#0000ff"],[.6,"#00ffff"],[.7,"#bfff00"],[.8,"#ffff00"],[1,"#ff0000"]],this.oGradientData=null,this.oBrush=null,this.sBrushType="circle",this.sBrushInfo=null,this.bSortData=!1,this.addData=function(t){this.aData.push(t)},this.setData=function(t,a){if(this.AAS&&-1===a.indexOf("scatter"))for(var e=0,i=t.length;e<i;e++)this.oKVData[t[e][0]+"/"+t[e][1]]=t[e][2];this.aData=t,this.aDataInit||(this.aDataInit=t),this.bSortData&&this.aData.sort(function(t,a){return t[2]-a[2]})},this.B4=null,this.BL=null,this.C5=.5,this.Z=null,this.AAS=null,this.setMinMax=function(){if(null===this.B4||null===this.BL)for(var t=0,a=this.aData.length;t<a;t++)this.B4=Math.min(this.B4,this.aData[t][2]),this.BL=Math.max(this.BL,this.aData[t][2])},this.createGradient=function(){var t=document.createElement("canvas"),a=t.getContext("2d"),e=a.createLinearGradient(0,0,0,256);t.width=1,t.height=256;for(var i=0;i<this.aGradient.length;i++)e.addColorStop(this.aGradient[i][0],this.aGradient[i][1]);a.fillStyle=e,a.fillRect(0,0,1,256),this.oGradientData=a.getImageData(0,0,1,256).data},this.createBrush=function(){var t=document.createElement("canvas"),a=t.getContext("2d");"circle"===this.sBrushType?t.width=t.height=2*(this.aSize[0]+this.iBlur):"square"===this.sBrushType?t.width=t.height=this.aSize[0]+2*this.iBlur:"rectangle"===this.sBrushType&&(t.width=this.aSize[0]+2*this.iBlur,t.height=this.aSize[1]+2*this.iBlur),a.shadowOffsetX=a.shadowOffsetY=200,a.shadowBlur=this.iBlur,a.shadowColor="#000","circle"===this.sBrushType?(a.beginPath(),a.arc(this.aSize[0]+this.iBlur-200,this.aSize[0]+this.iBlur-200,this.aSize[0],0,2*Math.PI,!0),a.closePath(),a.fill()):"square"===this.sBrushType?a.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[0]):"rectangle"===this.sBrushType&&a.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[1]),this.oBrush=t},this.reset=function(){this.aData=this.aDataInit},this.paint=function(){var t,a,e;if(this.aData.length){this.UID=this.UID?++this.UID:1;var i=this.Z.getContext("2d"),s=this,r=null,h=null;if(!this.AAS||((r=zingchart.getLoader(this.AAS.loaderid))&&(h=zingchart.getGraph(r,this.AAS.graphid)),h)){if(this.AAS){var n=zingchart.exec(this.AAS.loaderid,"getobjectinfo",{graphid:this.AAS.graphid,object:"plotarea"});n&&(i.beginPath(),i.rect(n.x,n.y,n.width,n.height),i.clip(),i.closePath());var l=zingchart.exec(this.AAS.loaderid,"getobjectinfo",{object:"scale",name:this.AAS.keyscale.name}),o=zingchart.exec(this.AAS.loaderid,"getobjectinfo",{object:"scale",name:this.AAS.valscale.name});if(l&&o){var c=(l.step+"").split(".");this.AAS.keyscale.decimals=2===c.length?c[1].length:0;var u=(o.step+"").split(".");this.AAS.valscale.decimals=2===u.length?u[1].length:0;var d=[],g=[],f=0,p=0,C=[];"xy"===h.AJ.layout?-1===h.AF.indexOf("scatter")&&(this.aSize=[Math.ceil(l.stepSize)+.5,Math.ceil(o.stepSize)+.5]):"radar"===h.AJ.layout&&(g=[l.x+l.width/2,l.y+l.height/2]);var m=h.BN(this.AAS.keyscale.name),v=h.BN(this.AAS.valscale.name),A=[],Z={},B=1;if(-1!==h.AF.indexOf("scatter")&&this.aData.length>1e5){var x=Math.max((m.BL-m.B4)/(m.X[m.X.length-1]-m.X[0]),(v.BL-v.B4)/(v.GY-v.FU));B=Math.max(1,Math.round(5*x))}for(t=0,a=this.aData.length;t<a;t++)if((e=this.aData[t])[0]>=m.B4&&e[0]<=m.BL&&e[1]>=v.B4&&e[1]<=v.BL){var y=m.B2(e[0]),D=v.B2(e[1]);"radar"===h.AJ.layout?(f=180*Math.atan2(y[1]-g[1],y[0]-g[0])/Math.PI,p=g[1]-D[1]+o.offsetStart,C=ZC.AO.BK(g[0],g[1],p,f),d.push([C[0],C[1],e[2],e[3]])):(d.push([y,D,e[2],e[3]]),-1!==h.AF.indexOf("scatter")&&(B>1?Z[B*Math.round(y/B)+"/"+B*Math.round(D/B)]=e:Z[Math.round(y)+"/"+Math.round(D)]=e))}if(-1!==h.AF.indexOf("scatter")){for(var _ in Z){var S=_.split("/");A.push({x:parseFloat(S[0]),y:parseFloat(S[1]),key:Z[_][0],val:Z[_][1],v:Z[_][2],info:Z[_][3]})}this.oKDTree=new ZC.ZCKDTree(A)}this.aData=d}}this.oGradientData||this.createGradient(),this.sBrushInfo||(this.sBrushInfo=[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")),this.oBrush&&this.sBrushInfo===[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")||(this.createBrush(),this.sBrushInfo=[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")),this.setMinMax(),i.clearRect(0,0,s.I,s.F);var z=this.LG?25:1,K=function(){var t=i.getImageData(0,0,s.I,s.F);i.clearRect(0,0,s.I,s.F);for(var a=3,e=t.data.length;a<e;a+=4)t.data[a]/=1/s.C5;i.putImageData(t,0,0),s.Z.style.opacity=1,"svg"===s.AC&&ZC.AK(s.Z.id.replace(/\-c$/,"-img")).setAttribute("href",s.Z.toDataURL())},M=this.UID,b=function(r,h,n){for(t=r;t<h;t++)if(e=s.aData[t]){var l=1;if(s.B4!==s.BL&&(l=Math.max((e[2]-s.B4)/(s.BL-s.B4),.01)),s.AAS&&!s.bComposite){var o=4*Math.floor(255*l),c=s.oGradientData[o],u=s.oGradientData[o+1],d=s.oGradientData[o+2];i.fillStyle="rgb("+c+","+u+","+d+")","circle"===s.sBrushType?(i.beginPath(),i.arc(e[0],e[1],s.aSize[0],0,2*Math.PI),i.fill(),i.closePath()):i.fillRect(e[0]-s.aSize[0]/2,e[1]-s.aSize[1]/2,s.aSize[0],s.aSize[1])}else i.globalAlpha=l,"circle"===s.sBrushType?i.drawImage(s.oBrush,e[0]-s.aSize[0]-s.iBlur,e[1]-s.aSize[0]-s.iBlur):"square"===s.sBrushType?i.drawImage(s.oBrush,e[0]-s.aSize[0]/2-s.iBlur,e[1]-s.aSize[0]/2-s.iBlur):"rectangle"===s.sBrushType&&i.drawImage(s.oBrush,e[0]-s.aSize[0]/2-s.iBlur,e[1]-s.aSize[1]/2-s.iBlur)}if(!s.AAS||s.bComposite){var g=i.getImageData(0,0,s.I,s.F);for(t=3,a=g.data.length;t<a;t+=4){var f=4*g.data[t];f&&(g.data[t-3]=s.oGradientData[f],g.data[t-2]=s.oGradientData[f+1],g.data[t-1]=s.oGradientData[f+2])}i.putImageData(g,0,0)}h=Math.min(s.aData.length,h+Math.round(s.aData.length/z)),r=Math.min(s.aData.length,r+Math.round(s.aData.length/z)),s.UID===M&&(!n&&s.LG?("svg"===s.AC&&ZC.AK(s.Z.id.replace(/\-c$/,"-img")).setAttribute("href",s.Z.toDataURL()),window.setTimeout(function(){b(r,h,h>=s.aData.length)},1)):K())};this.aData.length&&b(0,Math.round(this.aData.length/z))}}},this.clear=function(){this.aData=[]}},zingchart.bind(null,"postzoom",function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"heatmap"))for(var s=0,r=e.AH.length;s<r;s++){var h=e.AH[s];if(h.o.heatmap){var n=t[e.K+"-"+h.L];n.reset(),n.paint()}}}),zingchart.bind(null,"mousemove",function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"heatmap"))for(var s=0,r=e.AH.length;s<r;s++){var h=e.AH[s];if(h.o.heatmap&&ZC.DT(a.x,h.iX,h.iX+h.I)&&ZC.DT(a.y,h.iY,h.iY+h.F)){var n,l=t[e.K+"-"+h.L],o=null;if(l){var c,u="",d="",g=null;if(-1===h.AF.indexOf("scatter"))if(l.AAS&&!l.bComposite){if(ZC.DT(a.x,h.P.iX,h.P.iX+h.P.I)&&ZC.DT(a.y,h.P.iY,h.P.iY+h.P.F)){for(var f=zingchart.exec(a.id,"getxyinfo",{x:a.x,y:a.y}),p=0;p<f.length;p++)"key-scale"===f[p].infotype&&(u=f[p].scalevalue.toFixed(l.AAS.keyscale.decimals)),"value-scale"===f[p].infotype&&(d=f[p].scalevalue.toFixed(l.AAS.valscale.decimals));o=l.oKVData[u+"/"+d]}}else{var C=l.Z.getContext("2d").getImageData(0,0,l.I,l.F).data[4*(Math.round(a.x-h.iX)+Math.round(a.y-h.iY)*l.I)+3];C=Math.min(255,Math.round(C*(1/l.C5))),o=l.B4+(l.BL-l.B4)*C/255,C<=0&&(o=null)}else(c=l.oKDTree.getNearestNeighbour({x:a.x,y:a.y},20))&&(o=c.v,u=c.key,d=c.val,g=c.info);if(ZC.AP.BZ("heatmap.mousemove",e,{id:e.K,graphid:h.K,ev:a.ev,x:a.x,y:a.y,value:o}),l.A8)if(null!==ZC._n_(o)){if(a.ev.target&&"AREA"!==a.ev.target.tagName.toUpperCase()){var m=u,v=u,A=d,Z=d;if(l.AAS){var B=h.BN(l.AAS.keyscale.name);if(B){var x=B.ML();x["transform-date"]&&(x.utc=h.U9,x.timezone=h.N6,v=ZC.AP.GF(v,x,h.BM[0],!0))}var y=h.BN(l.AAS.valscale.name);y&&y.CI&&(Z=y.CI.replace(/%v|%scale-value/g,Z))}if(n={},ZC._cp_(l.A8,n),-1!==h.AF.indexOf("scatter")&&(n.x=c.x,n.y=c.y-("canvas"===e.AC?0:20),n.anchor="c",n.placement="xy"),l.A8["js-rule"])try{var D=ZC.execFn(l.A8["js-rule"].replace("javascript:","").replace("()",""),window,{key:u,val:d,v:o},g);D&&ZC._cp_(D,n)}catch(_){}o=zingchart.formatNumber(o,l.A8),n.text=n.text||o+"",-1!==n.text.indexOf("%")&&(n.text=n.text.replace(/%value/g,o+"").replace(/%scale-key-text/g,v).replace(/%scale-key-value/g,m).replace(/%scale-value-text/g,Z).replace(/%scale-value-value/g,A)),zingchart.exec(a.id,"showtooltip",{ev:a.ev,xy:!0,data:n})}}else a.ev.target&&"AREA"!==a.ev.target.tagName.toUpperCase()&&zingchart.exec(a.id,"hidetooltip",{})}}}}),zingchart.bind(null,"load",function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"heatmap"))for(var s=0,r=e.AH.length;s<r;s++){var h=e.AH[s];if(h.o.heatmap){var n=h.o.heatmap||{};h.o.options&&(n=h.o.options.heatmap||n),ZC._todash_(n);var l=document.createElement("canvas");l.id=h.K+"-heatmap-c",l.className="canvas"===e.AC?"":"zc-no-print",l.width=h.I,l.height=h.F;var o,c=ZC._c_(n.placement,n.graph?"bottom":"top");if("canvas"===e.AC)"top"===c?ZC.AK(e.K+"-tools").appendChild(l):(o=ZC.AK(h.K+"-plots-bl-0")||ZC.AK(h.K+"-plots-bl")).appendChild(l);else{var u=ZC.Q.F4("foreignObject","http://www.w3.org/2000/svg");u.id=h.K+"-heatmap",ZC.Q.G2(u,{x:h.iX,y:h.iY,width:h.I,height:h.F}),u.style.opacity=0,u.appendChild(l);var d=ZC.Q.F4("image","http://www.w3.org/2000/svg");d.id=h.K+"-heatmap-img",ZC.Q.G2(d,{x:h.iX,y:h.iY,width:h.I,height:h.F,href:""}),"top"===c?(ZC.AK(e.K+"-tools").appendChild(u),ZC.AK(e.K+"-tools").appendChild(d)):((o=ZC.AK(h.K+"-plots-bl-0")||ZC.AK(h.K+"-plots-bl")).appendChild(u),o.appendChild(d))}var g=t[e.K+"-"+h.L]=new ZC.ZCHeatMap;g.I=e.I,g.F=e.F,g.Z=l,g.AC=e.AC,"canvas"===e.AC&&(g.Z.style.position="absolute",g.Z.style.left=h.iX+"px",g.Z.style.top=h.iY+"px"),g.sBrushType=ZC._c_(n["brush-type"],"circle");var f=[[15,15],45];"square"===g.sBrushType?f=[[15,15],5]:"rectangle"===g.sBrushType&&(f=[[10,15],5]),g.aSize=ZC._c_(n.size,f[0]),"object"!=typeof g.aSize&&(g.aSize=[ZC._f_(g.aSize),ZC._f_(g.aSize)]),g.iBlur=ZC._i_(ZC._c_(n.blur,f[1])),g.LG=ZC._b_(ZC._c_(n.async,!1)),g.bSortData=ZC._b_(ZC._c_(n["sort-data"],!1)),g.C5=ZC._f_(ZC._c_(n.alpha,.5)),n.tooltip&&(g.A8=n.tooltip),ZC._n_(n["min-value"])&&(g.B4=ZC._f_(n["min-value"])),ZC._n_(n["max-value"])&&(g.BL=ZC._f_(n["max-value"])),g.Z.style.opacity=g.C5,n.graph&&(g.bComposite=!1,g.AAS={loaderid:e.K,graphid:h.K,keyscale:{name:"scale-x",decimals:0},valscale:{name:"scale-y",decimals:0}},n.graph["key-scale"]&&(g.AAS.keyscale.name=n.graph["key-scale"]),n.graph["val-scale"]&&(g.AAS.valscale.name=n.graph["val-scale"])),ZC._n_(n.composite)&&(g.bComposite=ZC._b_(n.composite));var p=["#0000ff","#00ffff","#bfff00","#ffff00","#ff0000"],C=[.4,.6,.7,.8,1];n["gradient-colors"]&&(p=n["gradient-colors"].split(/\s|;/)),n["gradient-stops"]&&(C=n["gradient-stops"].split(/\s|;/)),g.aGradient=[];for(var m=0;m<Math.min(p.length,C.length);m++)g.aGradient.push([C[m],p[m]]);g.paint()}}}),zingchart.extendAPI("heatmap.setdata",function(a,e){"string"==typeof(e=e||{})&&(e=JSON.parse(e)),ZC._todash_(e,!0,"data");var i=zingchart.getLoader(a);if(i){var s=i.C9(e.graphid);if(s){var r=i.K+"-"+s.L;t&&t[r]&&(null!==ZC._n_(e["min-value"])&&(t[r].B4=ZC._f_(e["min-value"])),null!==ZC._n_(e["max-value"])&&(t[r].BL=ZC._f_(e["max-value"])),null!==ZC._n_(e.size)&&(t[r].aSize=[ZC._f_(e.size),ZC._f_(e.size)]),null!==ZC._n_(e.blur)&&(t[r].iBlur=ZC._f_(e.blur)),t[r].setData(e.data||[],s.AF),t[r].paint())}}}),ZC.ZCKDTreeNode=function(t){this.aPoint=t,this.oLeftChild=null,this.oRightChild=null,this.oBounds=null},ZC.ZCKDTree=function(t){for(var a={minX:ZC.MAX,maxX:-ZC.MAX,minY:ZC.MAX,maxY:-ZC.MAX},e=0,i=t.length;e<i;e++)a.minX=Math.min(a.minX,t[e].x),a.maxX=Math.max(a.maxX,t[e].x),a.minY=Math.min(a.minY,t[e].y),a.maxY=Math.max(a.maxY,t[e].y);this.MF=ZC.ZCKDTree.createKDTree(t,0,a)},ZC.ZCKDTree.createKDTree=function(t,a,e){if(0===t.length)return null;var i=a%2;t.sort(function(t,a){return 0===i?t.x-a.x:t.y-a.y});var s=Math.floor(t.length/2),r=new ZC.ZCKDTreeNode(t[s]);r.oBounds=e;var h={minX:e.minX,maxX:0===i?r.aPoint.x:e.maxX,minY:e.minY,maxY:1===i?r.aPoint.y:e.maxY},n={minX:0===i?r.aPoint.x:e.minX,maxX:e.maxX,minY:1===i?r.aPoint.y:e.minY,maxY:e.maxY};return r.oLeftChild=ZC.ZCKDTree.createKDTree(t.slice(0,s),a+1,h),r.oRightChild=ZC.ZCKDTree.createKDTree(t.slice(s+1),a+1,n),r},ZC.ZCKDTree.getDistance=function(t,a,e,i){return Math.sqrt((t-e)*(t-e)+(a-i)*(a-i))},ZC.ZCKDTree.prototype.getNearestNeighbour=function(t,a,e){var i=[];return this._getNearestNeighbours_(this.MF,t,0,i,1,e),0===i.length?null:i[0].GW<=a?i[0].AW.aPoint:null},ZC.ZCKDTree.prototype.getNearestNeighbours=function(t,a,e,i){var s=[];this._getNearestNeighbours_(this.MF,t,0,s,a,i);for(var r=[],h=0,n=s.length;h<n;h++)s[h].GW<=e&&r.push(s[h].AW.aPoint);return r},ZC.ZCKDTree.prototype._getNearestNeighbours_=function(t,a,e,i,s,r){r&&r.push(t.aPoint);var h=e%2,n={AW:t,GW:ZC.ZCKDTree.getDistance(a.x,a.y,t.aPoint.x,t.aPoint.y)};ZC.ZCKDTree.insertResult(i,n,s);var l=(0===h?a.x:a.y)<(0===h?t.aPoint.x:t.aPoint.y),o=l?t.oLeftChild:t.oRightChild,c=l?t.oRightChild:t.oLeftChild;if(o&&this._getNearestNeighbours_(o,a,e+1,i,s,r),c){var u,d;0===h?(u=t.aPoint.x,d=a.y,a.y<c.oBounds.minY?d=c.oBounds.minY:a.y>c.oBounds.maxY&&(d=c.oBounds.maxY)):(u=a.x,d=t.aPoint.y,a.x<c.oBounds.minX?u=c.oBounds.minX:a.x>c.oBounds.maxX&&(u=c.oBounds.maxX)),ZC.ZCKDTree.getDistance(a.x,a.y,u,d)<=i[i.length-1].GW&&this._getNearestNeighbours_(c,a,e+1,i,s,r)}},ZC.ZCKDTree.insertResult=function(t,a,e){var i;for(i=t.length-1;i>=0;--i){var s=t[i];if(a.GW>s.GW)break}t.splice(i+1,0,a),t.length>e&&t.pop()}}}();
